package com.cn.psys.system.controller;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.date.DateUtil;
import com.cn.psys.system.entity.JhfbStu;
import com.cn.psys.system.service.IJhfbStuService;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.cn.psys.system.service.IStuInfoService;
import com.cn.psys.system.entity.StuInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.util.List;
import com.cn.psys.base.BaseResponse;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.cn.psys.mybatisplus.MybatisPlusQuery;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import cn.hutool.core.util.IdUtil;
/**
* <p>
    *  前端控制器
    * </p>
*
* @author zhuwei
* @since 2020-08-31
*/
@RestController
@RequestMapping("/stu-info")
    public class StuInfoController {
    @Autowired
    private IStuInfoService targetService;
    @Autowired
    private IJhfbStuService targetServiceInfo;


    /**
    * showdoc
    * @catalog StuInfo
    * @title 分页列表
    * @description 分页列表接口
    * @method post
    * @url http://localhost/user/sys-user/list
    * @param page 非必选 string 页数（不传默认第一页）
    * @param rows 非必选 string 行数（不传默认一页10行）
    * @remark 无
    * @number 1
    */
    @RequiresPermissions("StuInfo:list")
    @RequestMapping("/list")
    @ResponseBody
    public BaseResponse findListByPage(@RequestParam(required=false) String paramData,@RequestParam(name = "page", defaultValue = "1") int pageIndex,@RequestParam(name = "rows", defaultValue = "10") int step){
        QueryWrapper<StuInfo> queryWrapper = MybatisPlusQuery.parseWhereSql(paramData,true);
        Page page = new Page(pageIndex,step);
        return BaseResponse.success(targetService.page(page,queryWrapper));
    }


    /**
    * showdoc
    * @catalog StuInfo
    * @title 所有数据列表
    * @description 所有数据列表
    * @method post
    * @url http://localhost/user/sys-user/all
    * @param 无 无 无 无
    * @remark 无
    * @number 2
    */
    @RequiresPermissions("StuInfo:all")
    @RequestMapping("/all")
    @ResponseBody
    public BaseResponse findAll(@RequestParam(required=false) String paramData) {
        QueryWrapper<StuInfo> queryWrapper = MybatisPlusQuery.parseWhereSql(paramData);
        return BaseResponse.success(targetService.list(queryWrapper));
     }


    /**
    * showdoc
    * @catalog StuInfo
    * @title 详情页面
    * @description 详情页面接口
    * @method post
    * @url http://localhost/user/sys-user/find
    * @param id 必选 Long 页数
    * @remark 无
    * @number 3
    */
    @RequiresPermissions("StuInfo:find")
    @RequestMapping("/find")
    @ResponseBody
    public BaseResponse find(@RequestParam(required=false) String paramData) {
        QueryWrapper<StuInfo> queryWrapper = MybatisPlusQuery.parseWhereSql(paramData);
        StuInfo StuInfo = null;
        try{
            StuInfo = targetService.getOne(queryWrapper,true);
        }catch (Exception e){
            e.printStackTrace();
        }
        if (StuInfo == null) {
            return BaseResponse.error("无查询结果");
        }
        return BaseResponse.success(StuInfo);
    }

    /**
    * showdoc
    * @catalog StuInfo
    * @title 新增数据
    * @description 新增数据接口
    * @method post
    * @url http://localhost/user/sys-user/add
    * @param StuInfo 必选 StuInfo 保存对象
    * @remark 无
    * @number 4
    */
    @RequiresPermissions("StuInfo:add")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public BaseResponse addItem(@RequestBody StuInfo StuInfo){
        String uuid = IdUtil.randomUUID();
        StuInfo.setUuid(uuid);
        boolean isOk = targetService.save(StuInfo);
        if(isOk){
            return BaseResponse.success(uuid);
        }
            return BaseResponse.error("数据添加失败");
    }


    /**
    * showdoc
    * @catalog StuInfo
    * @title 更新数据
    * @description 更新数据接口
    * @method post
    * @url http://localhost/user/sys-user/add
    * @param StuInfo 必选 StuInfo 保存对象
    * @remark 无
    * @number 5
    */
    @RequiresPermissions("StuInfo:update")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public BaseResponse updateItem(@RequestBody StuInfo StuInfo){
        boolean isOk = targetService.updateById(StuInfo);
        if(isOk){
            return BaseResponse.success("数据更改成功！");
        }
            return BaseResponse.error("数据更改失败");
    }


    /**
    * showdoc
    * @catalog StuInfo
    * @title 删除数据
    * @description 删除数据接口
    * @method post
    * @url http://localhost/user/sys-user/del
    * @param ids 必选 List<Long> 页数
        * @remark 无
        * @number 7
        */
        @RequiresPermissions("StuInfo:del")
        @RequestMapping("/del")
        @ResponseBody
        public BaseResponse deleteItems(@RequestParam("ids") List<String> ids){
            boolean isOk = targetService.removeByIds(ids);
            if(isOk){
                return BaseResponse.success("数据删除成功！");
            }
                return BaseResponse.error("数据删除失败");
            }

    @RequestMapping("/copyStu")
    @ResponseBody
    public BaseResponse copyStu(@RequestParam("proId") String proId){
        String paramData = "[{column: \"pro_id\",type: \"eq\", value: \""+proId+"\"}]";
        try{
            QueryWrapper<StuInfo> query = MybatisPlusQuery.parseWhereSql(paramData);
            List<StuInfo> checkList = targetService.list(query);
            if(checkList.size()>0){
                return BaseResponse.error("请勿重复操作！");
            }

            QueryWrapper<JhfbStu> queryWrapper = MybatisPlusQuery.parseWhereSql(paramData);
            List<JhfbStu> list_jhfbstu = targetServiceInfo.list(queryWrapper);
            StuInfo inserObj;
            String createUser = (String) SecurityUtils.getSubject().getPrincipal();
            for (JhfbStu tmp : list_jhfbstu){
                inserObj = new StuInfo();
                inserObj.setUuid(IdUtil.randomUUID());
                inserObj.setCreateTime(DateUtil.now());
                inserObj.setAllScore(tmp.getAllScore());
                inserObj.setCardNum(tmp.getCardNum());
                inserObj.setEnglishScore(tmp.getEnglishScore());
                inserObj.setGraduateSchool(tmp.getGraduateSchool());
                inserObj.setHisScore(tmp.getHisScore());
                inserObj.setLanguageScore(tmp.getLanguageScore());
                inserObj.setMathScore(tmp.getMathScore());
                inserObj.setLeagueMembers(tmp.getLeagueMembers());
                inserObj.setNation(tmp.getNation());
                inserObj.setNewClass(tmp.getNewClass());
                inserObj.setTicketNumber(tmp.getTicketNumber());
                inserObj.setProId(tmp.getProId());
                inserObj.setRepCity(tmp.getRepCity());
                inserObj.setSex(tmp.getSex());
                inserObj.setStuName(tmp.getStuName());
                inserObj.setStuNumber(tmp.getStuNumber());
                inserObj.setSportScore(tmp.getSportScore());
                inserObj.setCreateUser(createUser);
                targetService.save(inserObj);
            }
        }catch (Exception e){
            e.printStackTrace();
        }


        return BaseResponse.success("操作成功");
    }
    }
